package com.mls.fivedistrict.server.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mls.fivedistrict.dao.GoodsImagesMapper;
import com.mls.fivedistrict.dao.GoodsMapper;
import com.mls.fivedistrict.dao.GoodsPayIntegralMapper;
import com.mls.fivedistrict.pojo.Goods;
import com.mls.fivedistrict.pojo.GoodsImages;
import com.mls.fivedistrict.pojo.GoodsPayIntegral;
import com.mls.fivedistrict.server.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Five Team
 * @since 2018-05-15
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsPayIntegralMapper goodsPayIntegralMapper;
    @Autowired
    GoodsImagesMapper goodsImagesMapper;
    /**
     * 动态插入支付支持的积分
     * 插入商品实体
     * @param goods
     * @return
     */
    @Override
    @Transactional
    public boolean    insert(Goods goods) {
        //商品实体类携带的支持积分列表
        List<GoodsPayIntegral> goodsPayIntegralList = goods.getGoodsPayIntegralList();
        List<GoodsImages> goodsImagesList = goods.getGoodsImages();
        Integer result = goodsMapper.insert(goods);
        if(goodsPayIntegralList.size()>0){
            for (GoodsPayIntegral goodsPayIntegral:goodsPayIntegralList) {
                goodsPayIntegral.setGoodsId(goods.getId());
            }
            goodsPayIntegralMapper.insertList(goodsPayIntegralList);
        }
        if(goodsImagesList.size()>0){
            for (GoodsImages goodsImages: goodsImagesList) {
                goodsImages.setGoodsId(goods.getId());
            }
            goodsImagesMapper.insertList(goodsImagesList);
        }
        if(null != result && result >= 1){
            return  true;
        }
        return  false;
    }

    /**
     * 修改商品
     * @param goods
     * @return
     */
    @Transactional
    @Override
    public boolean updateById(Goods goods) {
        Integer result = goodsMapper.updateById(goods);
        //修改前的支持积分列表
        List<GoodsPayIntegral>  before= goodsPayIntegralMapper.selectListByGoodsId(goods.getId());
        //修改后的支持积分列表
        List<GoodsPayIntegral>  after= goods.getGoodsPayIntegralList();
        //修改前有积分,先删除之前的再新增之后的
        if(null!=before&&before.size()>0){
            goodsPayIntegralMapper.deleteByGoodsId(goods.getId());
        }
        //修改前没积分直接新增
        if(null!=after&&after.size()>0){
            for (GoodsPayIntegral goodsPayIntegral:after) {
                goodsPayIntegral.setGoodsId(goods.getId());
            }
            goodsPayIntegralMapper.insertList(after);
        }
        //修改前的商品图片列表
        List<GoodsImages> goodsImagesBefore = goodsImagesMapper.selectListByGoodsId(goods.getId());
        //修改后的商品图片列表
        List<GoodsImages> goodsImagesAfter = goods.getGoodsImages();
        //修改前有图片,先删除之前的再新增之后的
        if(null!=goodsImagesBefore&&goodsImagesBefore.size()>0){
            goodsImagesMapper.deleteByGoodsId(goods.getId());
        }
        //修改前没图片直接新增
        if(null!=goodsImagesAfter&&goodsImagesAfter.size()>0){
            for (GoodsImages goodsImages:goodsImagesAfter) {
                goodsImages.setGoodsId(goods.getId());
            }
            goodsImagesMapper.insertList(goodsImagesAfter);
        }
        if(null != result && result >= 1){
            return  true;
        }
        return  false;
    }

    @Override
    public Goods selectById(Serializable id) {
        return goodsMapper.selectById(id);
    }
}
